package com.account.book.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.account.book.common.context.UserTokenContextHolder;
import com.account.book.common.exception.BasicInfoException;
import com.account.book.common.exception.BasicInfoStatusEnum;
import com.account.book.entity.Book;
import com.account.book.entity.BookUser;
import com.account.book.enums.BookAuthEnum;
import com.account.book.enums.BookUserAuditStatusEnum;
import com.account.book.enums.DataStatusEnum;
import com.account.book.mapper.BookUserMapper;
import com.account.book.request.BookUserQueryReqVo;
import com.account.book.request.UserApplyBookReqVo;
import com.account.book.response.BookResVo;
import com.account.book.response.BookUserQueryVo;
import com.account.book.response.BookUserResVo;
import com.account.book.service.ApprovalInfoService;
import com.account.book.service.BookService;
import com.account.book.service.BookUserService;
import com.account.book.service.UserService;
import com.account.book.vo.BookUserEditVo;
import com.account.book.vo.BookUserVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wky.sensitive.annotation.SensitiveReplace;
import com.wky.sensitive.enums.DataTypeEnum;
import ma.glasnost.orika.MapperFacade;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author liang-hw
 * @className BookUserServiceImpl
 * @description 账本用户表
 * @date 2023/11/09 13:20:15
 */
@Service
public class BookUserServiceImpl extends ServiceImpl<BookUserMapper, BookUser> implements BookUserService {

    private static final Logger logger = LoggerFactory.getLogger(BookUserServiceImpl.class);
    @Autowired
    private BookService bookService;

    @Autowired
    private UserService userService;

    @Autowired
    private BookUserMapper bookUserMapper;

    @Autowired
    private ReplaceDataApi replaceDataApi;

    @Autowired
    private ApprovalInfoService approvalInfoService;
    @Autowired
    private MapperFacade mapperFacade;

    /**
     * @param @param bookId
     * @return @return {@link BookUserResVo }
     * @author liang-hw
     * @description 查询账本下的用户列表
     * @date 2023/11/09 13:20:44
     */
    @Override
    public BookUserResVo getBookUser(String bookId) {
        Book book = bookService.getById(bookId);
        if (null == book) {
            logger.error("查询账本：{}不存在", bookId);
            throw new BasicInfoException(BasicInfoStatusEnum.SYSTEM_ERROR.getCode(), "账本不存在");
        }
        BookUserResVo bookUserResVo = new BookUserResVo();
        bookUserResVo.setBook(book);

        List<BookUser> bookUsers = this.list(new QueryWrapper<BookUser>().lambda().eq(BookUser::getStatus, DataStatusEnum.ENABLE.getValue()).eq(BookUser::getBookId, bookId).ne(BookUser::getAuditStatus, BookUserAuditStatusEnum.REJECT.getValue()).orderByDesc(BookUser::getAuth));
        List<BookUserQueryVo> bookResVoList = mapperFacade.mapAsList(bookUsers, BookUserQueryVo.class);
        bookUserResVo.setBookUserQueryVos(bookResVoList);
        return bookUserResVo;
    }


    /**
     * @param @param bookId
     * @param userId
     * @return @return {@link Boolean }
     * @author liang-hw
     * @description 判断用户是否有该账本权限
     * @date 2024/01/10 11:25:45
     */
    @Override
    public BookUserQueryVo isBookAuthor(String bookId, String userId) {
        BookUser bookUser = this.getOne(new QueryWrapper<BookUser>().lambda()
                .eq(BookUser::getBookId, bookId)
                .eq(BookUser::getUserId, userId)
                .eq(BookUser::getStatus, DataStatusEnum.ENABLE.getValue())
        );
        BookUserQueryVo bookUserQueryVo = mapperFacade.map(bookUser, BookUserQueryVo.class);
        return bookUserQueryVo;
    }

    /**
     * @param @param bookId
     * @return @return {@link BookUserQueryVo }
     * @author liang-hw
     * @description 查询账本下的用户信息
     * @date 2023/12/04 16:49:41
     */
    @Override
    public BookUserQueryVo getBookUserInfo(String bookId, String userId) {
        BookUserQueryVo bookUserQueryVo = new BookUserQueryVo();
        Book book = bookService.getById(bookId);
        if (null == book) {
            logger.error("查询账本：{}不存在", bookId);
            throw new BasicInfoException(BasicInfoStatusEnum.SYSTEM_ERROR.getCode(), "账本不存在");
        }
        BookUserResVo bookUserResVo = new BookUserResVo();
        bookUserResVo.setBook(book);
        // 当前请求账本数据人是否是账本成员
        BookUser bookUser = baseMapper.selectOne((new QueryWrapper<BookUser>()).lambda()
                .eq(BookUser::getUserId, userId)
                .eq(BookUser::getBookId, bookId)
                .eq(BookUser::getStatus, DataStatusEnum.ENABLE.getValue()));
        BeanUtils.copyProperties(bookUser, bookUserQueryVo);
        return bookUserQueryVo;
    }

    /**
     * @param @param bookUserQueryReqVo
     * @return
     * @author liang-hw
     * @description 修改当前账本预算
     * @date 2024/01/10 16:45:51
     */
    @Override
    public void editBudget(BookUserQueryReqVo bookUserQueryReqVo, String userId) {
        UpdateWrapper<BookUser> bookUserUpdateWrapper = new UpdateWrapper<>();
        bookUserUpdateWrapper.lambda().eq(BookUser::getBookId, bookUserQueryReqVo.getBookId())
                .eq(BookUser::getUserId, userId)
                .eq(BookUser::getStatus, DataStatusEnum.ENABLE.getValue())
                .set(BookUser::getBudget, bookUserQueryReqVo.getBudget());
        this.update(null, bookUserUpdateWrapper);
    }


    @Override
    @SensitiveReplace(dataType = DataTypeEnum.LIST)
    public List<BookUserResVo> replaceList(String bookId) {
        List<BookUser> bookUsers = this.list(new QueryWrapper<BookUser>().lambda().eq(BookUser::getStatus, DataStatusEnum.ENABLE.getValue()).eq(BookUser::getBookId, bookId).ne(BookUser::getAuditStatus, BookUserAuditStatusEnum.REJECT.getValue()).orderByDesc(BookUser::getAuth));
        List<BookUserResVo> bookUserResVoList = mapperFacade.mapAsList(bookUsers, BookUserResVo.class);
        return bookUserResVoList;
    }

    /**
     * @param @param bookId
     * @return @return {@link BookUserResVo }
     * @author liang-hw
     * @description 查询账本申请加入的用户
     * @date 2023/11/09 13:22:44
     */
    @Override
    public BookUserResVo bookAudit(String bookId) {
        Book bookServiceById = bookService.getById(bookId);
        if (null == bookServiceById) {
            logger.error("审核账本：{}不存在", bookId);
            throw new BasicInfoException(BasicInfoStatusEnum.SYSTEM_ERROR.getCode(), "账本不存在");
        }
        BookUserResVo bo = new BookUserResVo();
        BeanUtil.copyProperties(bookServiceById, bo);
        List<BookUser> bookUsers = this.list(new QueryWrapper<BookUser>().lambda().eq(BookUser::getStatus, DataStatusEnum.DISABLE.getValue()).eq(BookUser::getAuditStatus, BookUserAuditStatusEnum.REJECT.getValue()).orderByDesc(BookUser::getAuth).eq(BookUser::getBookId, bookId));
        List<BookUserQueryVo> bookResVoList = mapperFacade.mapAsList(bookUsers, BookUserQueryVo.class);
        bo.setBookUserQueryVos(bookResVoList);
        return bo;
    }

    /**
     * @param @param bookUserEditVo
     * @return
     * @author liang-hw
     * @description 编辑成员
     * @date 2023/11/09 13:22:54
     */
    @Override
    public void editBookUser(BookUserEditVo bookUserEditVo) {
        BookUser editBookUser = isEditBookUser(bookUserEditVo);
        BeanUtils.copyProperties(bookUserEditVo, editBookUser);
        editBookUser.setUpdateBy(UserTokenContextHolder.getUserTokenVOByToken().getUserId());
    }

    /**
     * @param @param bookUserEditVo
     * @return
     * @author liang-hw
     * @description 修改用户，目前只支持修改用户名称跟头像
     * @date 2023/11/09 13:23:39
     */
    @Override
    public void update(BookUserEditVo bookUserEditVo) {
        if (StringUtils.isNotBlank(bookUserEditVo.getUserName()) && bookUserEditVo.getUserName().length() > 50) {
            logger.error("用户名太长：{}", bookUserEditVo.getUserName());
            throw new BasicInfoException(BasicInfoStatusEnum.SYSTEM_ERROR.getCode(), "用户名太长");
        }
        if (StringUtils.isNotBlank(bookUserEditVo.getReallyName()) && bookUserEditVo.getReallyName().length() > 50) {
            logger.error("用户别名太长：{}", bookUserEditVo.getReallyName());
            throw new BasicInfoException(BasicInfoStatusEnum.SYSTEM_ERROR.getCode(), "用户别名太长");
        }

        // TODO: 2022/4/30 0030  book_user 需要建立 book_id与 userName联合唯一索引
        // 查询账本下是否存在当前用户
        BookUser repeatBookUser = baseMapper.selectOne(new QueryWrapper<BookUser>().lambda().eq(BookUser::getBookId, bookUserEditVo.getBookId()).eq(BookUser::getStatus, DataStatusEnum.ENABLE.getValue()).eq(BookUser::getUserName, bookUserEditVo.getUserName()).ne(BookUser::getUserId, bookUserEditVo.getUserId()));
        if (null != repeatBookUser) {
            logger.error("账本下存在：{}相同名称", bookUserEditVo.getUserName());
            throw new BasicInfoException(BasicInfoStatusEnum.SYSTEM_ERROR.getCode(), "该名称已存在");
        }
        BookUser editBookUser = isEditBookUser(bookUserEditVo);
        // 只能修改用户名称跟头像
        editBookUser.setUserName(StringUtils.isEmpty(bookUserEditVo.getUserName()) ? null : bookUserEditVo.getUserName());
        // 只能修改用户名称跟头像
        editBookUser.setAvatarUrl(StringUtils.isEmpty(bookUserEditVo.getAvatarUrl()) ? null : bookUserEditVo.getAvatarUrl());

        // 修改人
        editBookUser.setUpdateBy(UserTokenContextHolder.getUserTokenVOByToken().getUserId());
        baseMapper.updateById(editBookUser);
    }

    /**
     * 1、用户权限只能改自己
     * 2、管理员只能改用户与自己
     * 3、群主改任何
     *
     * @param bookUserEditVo
     */
    private BookUser isEditBookUser(BookUserEditVo bookUserEditVo) {
        //
        String nowId = UserTokenContextHolder.getUserTokenVOByToken().getUserId();
        // 查询当前操作人
        BookUser nowBookUser = baseMapper.selectOne(new QueryWrapper<BookUser>().lambda().eq(BookUser::getUserId, nowId).eq(BookUser::getStatus, DataStatusEnum.ENABLE.getValue()).eq(BookUser::getBookId, bookUserEditVo.getBookId()));
        // 获取被修改人
        BookUser updateBookUser = baseMapper.selectOne(new QueryWrapper<BookUser>().lambda().eq(BookUser::getUserId, bookUserEditVo.getUserId()).eq(BookUser::getStatus, DataStatusEnum.ENABLE.getValue()).eq(BookUser::getBookId, bookUserEditVo.getBookId()));
        if (nowBookUser == null) {
            logger.error("账本修改失败：操作人不存在：操作人id：{}", nowId);
            throw new BasicInfoException(BasicInfoStatusEnum.SYSTEM_ERROR.getCode(), "操作人不存在");
        }
        // 操作人是群主权限
        if (nowBookUser.getAuth().longValue() == BookAuthEnum.ROOT.getValue().longValue()) {
            return updateBookUser;
        }
        // 如果是自己可修改
        if (updateBookUser.getUserId().equals(nowId)) {
            return updateBookUser;
        }
        // 操作人是管理员权限
        if (nowBookUser.getAuth().longValue() == BookAuthEnum.ADMIN.getValue().longValue()) {
            // 被修改人不是用户
            if (updateBookUser.getAuth().longValue() != BookAuthEnum.USER.getValue().longValue()) {
                logger.error("账管理员只能修改自己与用户，管理员id：{}", nowId);
                throw new BasicInfoException(BasicInfoStatusEnum.SYSTEM_ERROR.getCode(), "只能修改用户");
            }
        }
        // 操作人是用户权限
        if (nowBookUser.getAuth().longValue() == BookAuthEnum.USER.getValue().longValue()) {
            // 被修改人不是自己
            if (updateBookUser.getUserId().equals(nowId)) {
                logger.error("用户没有权限修改他人信息，用户id：{}", nowId);
                throw new BasicInfoException(BasicInfoStatusEnum.SYSTEM_ERROR.getCode(), "用户没有权限");
            }
        }
        return updateBookUser;
    }

    /**
     * @param @param bookId
     * @param userId
     * @return
     * @author liang-hw
     * @description 删除成员
     * @date 2023/11/09 13:23:04
     */
    @Override
    public void remove(String bookId, String editUserId, String userId) {
        BookUser bookUser = isDeleteBookUser(bookId, editUserId, userId);
        bookUser.setStatus(DataStatusEnum.DELETE.getValue());
        bookUser.setAuth(BookAuthEnum.USER.getValue());
        bookUser.setUpdateBy(userId);
        baseMapper.updateById(bookUser);
    }

    private BookUser isDeleteBookUser(String bookId, String editUserId, String userId) {
        BookUser bookUser = baseMapper.selectOne(new QueryWrapper<BookUser>().lambda().eq(BookUser::getUserId, editUserId).eq(BookUser::getStatus, DataStatusEnum.ENABLE.getValue()).eq(BookUser::getBookId, bookId));
        if (null == bookUser) {
            logger.error("账本无该用户，用户id：{}", editUserId);
            throw new BasicInfoException(BasicInfoStatusEnum.SYSTEM_ERROR.getCode(), "账本无该用户");
        }
        // 查询当前操作人
        BookUser authBook = baseMapper.selectOne(new QueryWrapper<BookUser>().lambda().eq(BookUser::getUserId, userId).eq(BookUser::getStatus, DataStatusEnum.ENABLE.getValue()).eq(BookUser::getBookId, bookId));
        // 仅群主管理员可以操作
        if (BookAuthEnum.USER.getValue().longValue() == authBook.getAuth().longValue()) {
            logger.error("用户无权删除，用户id：{}", userId);
            throw new BasicInfoException(BasicInfoStatusEnum.SYSTEM_ERROR.getCode(), "无权限操作");
        }
        if (userId.equals(editUserId)) {
            logger.error("用户不能移除自己，用户id：{}", userId);
            throw new BasicInfoException(BasicInfoStatusEnum.SYSTEM_ERROR.getCode(), "不能移除自己");
        }
        // 如果是群主
        if (null != authBook && bookUser.getAuth().longValue() == BookAuthEnum.ROOT.getValue().longValue()) {
            logger.error("不能删除群主，用户id：{}", userId);
            throw new BasicInfoException(BasicInfoStatusEnum.SYSTEM_ERROR.getCode(), "不能删除群主");
        }
        // 管理员想删除群主
        if (BookAuthEnum.ADMIN.getValue().intValue() == authBook.getAuth().longValue()) {
            if (bookUser.getAuth().longValue() == BookAuthEnum.ROOT.getValue().longValue()) {
                logger.error("管理员不能删除群主，管理员id：{}", userId);
                throw new BasicInfoException(BasicInfoStatusEnum.SYSTEM_ERROR.getCode(), "想造反？");
            }
            if (bookUser.getAuth().longValue() == BookAuthEnum.ADMIN.getValue().longValue()) {
                logger.error("不可删除管理，管理员id：{}", userId);
                throw new BasicInfoException(BasicInfoStatusEnum.SYSTEM_ERROR.getCode(), "不可删除管理");
            }
        }
        return bookUser;
    }


    /**
     * @param @param bookId
     * @param userId
     * @return
     * @author liang-hw
     * @description 添加成员为管理员
     * @date 2023/11/09 13:23:17
     */
    @Override
    public void addAuth(String bookId, String editUserId, String userId) {
        BookUser bookUser = rootBookUser(bookId, editUserId, userId);
        bookUser.setAuth(BookAuthEnum.ADMIN.getValue());
        bookUser.setUpdateBy(userId);
        baseMapper.updateById(bookUser);
    }

    /**
     * @param @param bookId
     * @param userId
     * @return
     * @author liang-hw
     * @description 移除管理员
     * @date 2023/11/09 13:23:27
     */
    @Override
    public void removeAuth(String bookId, String editUserId, String userId) {
        BookUser bookUser = rootBookUser(bookId, editUserId, userId);
        bookUser.setAuth(BookAuthEnum.USER.getValue());
        bookUser.setUpdateBy(userId);
        baseMapper.updateById(bookUser);
    }

    private BookUser rootBookUser(String bookId, String editUserId, String userId) {
        // 只有群主有权限
        // 查询当前操作人
        BookUser nowBookUser = baseMapper.selectOne(new QueryWrapper<BookUser>().lambda().eq(BookUser::getStatus, DataStatusEnum.ENABLE.getValue()).eq(BookUser::getUserId, userId).eq(BookUser::getBookId, bookId));
        // 获取被修改人
        BookUser updateBookUser = baseMapper.selectOne(new QueryWrapper<BookUser>().lambda().eq(BookUser::getUserId, editUserId).eq(BookUser::getStatus, DataStatusEnum.ENABLE.getValue()).eq(BookUser::getBookId, bookId));
        if (nowBookUser == null) {
            throw new BasicInfoException(BasicInfoStatusEnum.SYSTEM_ERROR.getCode(), "操作人不存在");
        }
        // 操作人是群主权限,且被修改人不是群主
        if (nowBookUser.getAuth().longValue() == BookAuthEnum.ROOT.getValue().longValue() && updateBookUser.getAuth().longValue() != BookAuthEnum.ROOT.getValue().longValue()) {
            // 返回被修改人
            return updateBookUser;
        }
        throw new BasicInfoException(BasicInfoStatusEnum.SYSTEM_ERROR.getCode(), "仅群主设置");
    }


    /**
     * @param @param bookId
     * @return
     * @author liang-hw
     * @description 用户自己退出账本
     * @date 2023/11/09 13:23:48
     */
    @Override
    public void signOutBook(String bookId, String userId) {
        Long aLong = bookService.isBookMember(bookId, userId);
        if (aLong == 0) {
            logger.error("请勿重复退出账本，用户id：{}", userId);
            throw new BasicInfoException(BasicInfoStatusEnum.SYSTEM_ERROR.getCode(), "请勿重复退出");
        }
        BookUser update = baseMapper.selectOne(new QueryWrapper<BookUser>().lambda().eq(BookUser::getUserId, userId).eq(BookUser::getBookId, bookId));
        update.setStatus(DataStatusEnum.DELETE.getValue());
        baseMapper.updateById(update);
    }


    /**
     * @param @param userApplyBookReqVo
     * @return
     * @author liang-hw
     * @description 拒绝用户加入账本
     * @date 2023/11/09 13:21:48
     */
    @Override
    @Transactional
    public void reject(UserApplyBookReqVo userApplyBookReqVo, String userId) {
        isRejectAndAgree(userApplyBookReqVo.getBookId(), userId);
        // 查询待审核状态
        BookUser nowBookUser = baseMapper.selectOne(new QueryWrapper<BookUser>().lambda().eq(BookUser::getStatus, DataStatusEnum.DISABLE.getValue()).eq(BookUser::getUserId, userApplyBookReqVo.getUserId()).eq(BookUser::getBookId, userApplyBookReqVo.getBookId()));
        nowBookUser.setStatus(DataStatusEnum.DISABLE.getValue());
        nowBookUser.setUpdateBy(userId);
        nowBookUser.setRemark(userApplyBookReqVo.getRemark());
        nowBookUser.setAuditStatus(BookUserAuditStatusEnum.REJECT.getValue());
        this.baseMapper.updateById(nowBookUser);
        approvalInfoService.addApprovalInfo(userApplyBookReqVo, userId, 0);
    }


    /**
     * @param @param userApplyBookReqVo
     * @return
     * @author liang-hw
     * @description 同意用户加入账本
     * @date 2023/11/09 13:21:35
     */
    @Override
    @Transactional
    public void agree(UserApplyBookReqVo userApplyBookReqVo, String userId) {
        isRejectAndAgree(userApplyBookReqVo.getBookId(), userId);
        // 查询待审核状态
        BookUser nowBookUser = baseMapper.selectOne(new QueryWrapper<BookUser>().lambda().eq(BookUser::getBookId, userApplyBookReqVo.getBookId()).eq(BookUser::getStatus, DataStatusEnum.DISABLE.getValue()).eq(BookUser::getUserId, userApplyBookReqVo.getUserId()));
        nowBookUser.setStatus(DataStatusEnum.ENABLE.getValue());
        nowBookUser.setAuditStatus(BookUserAuditStatusEnum.ARGEE.getValue());
        nowBookUser.setRemark(userApplyBookReqVo.getRemark());
        nowBookUser.setUpdateBy(userId);
        this.baseMapper.updateById(nowBookUser);
        approvalInfoService.addApprovalInfo(userApplyBookReqVo, userId, 1);
    }

    private void isRejectAndAgree(String bookId, String userId) {
        // 查询当前操作人
        BookUser nowBookUser = baseMapper.selectOne(new QueryWrapper<BookUser>().lambda().eq(BookUser::getUserId, userId).eq(BookUser::getStatus, DataStatusEnum.ENABLE.getValue()).eq(BookUser::getBookId, bookId));
        if (null == nowBookUser) {
            logger.error("拒绝用户加入失败，操作人不存在，操作人id：{}", UserTokenContextHolder.getUserTokenVOByToken().getUserId());
            throw new BasicInfoException(BasicInfoStatusEnum.SYSTEM_ERROR.getCode(), "操作人不存在");
        }
        if (Objects.equals(BookAuthEnum.USER.getValue(), nowBookUser.getAuth())) {
            logger.error("成员没有权限拒绝用户加入失败，操作成员人id：{}", UserTokenContextHolder.getUserTokenVOByToken().getUserId());
            throw new BasicInfoException(BasicInfoStatusEnum.SYSTEM_ERROR.getCode(), "成员没有权限");
        }
    }

    /**
     * @param
     * @return @return {@link Integer }
     * @author liang-hw
     * @description 获取待审核的数量
     * @date 2023/11/09 13:22:04
     */
    @Override
    public Integer getAuditCount(String userId) {
        // 查询当前用户的管理的账本
        List<BookUser> bookUsers = baseMapper.selectList(new QueryWrapper<BookUser>().lambda().eq(BookUser::getStatus, DataStatusEnum.ENABLE.getValue()).eq(BookUser::getUserId, userId).ne(BookUser::getAuth, BookAuthEnum.USER.getValue()));
        List<String> bookIds = bookUsers.stream().map(x -> x.getBookId()).collect(Collectors.toList());
        if (bookIds.size() == 0) {
            return bookIds.size();
        }
        return baseMapper.selectCount(new QueryWrapper<BookUser>().lambda().in(BookUser::getBookId, bookIds).eq(BookUser::getAuditStatus, BookUserAuditStatusEnum.DEFAULT.getValue()).eq(BookUser::getStatus, DataStatusEnum.DISABLE.getValue())).intValue();
    }


    /**
     * @param
     * @return @return {@link List }<{@link BookUser }>
     * @author liang-hw
     * @description 查询所有申请加入的用户
     * @date 2023/11/09 13:22:23
     */
    @Override
    public List<BookUserQueryVo> getAuditAll(String userId) {
        // 查询当前用户的管理的账本
        List<String> bookIds = baseMapper.selectList(new QueryWrapper<BookUser>().lambda().eq(BookUser::getStatus, DataStatusEnum.ENABLE.getValue()).eq(BookUser::getUserId, userId).ne(BookUser::getAuth, BookAuthEnum.USER.getValue())).stream().map(x -> x.getBookId()).collect(Collectors.toList());
        // 查询正常的账本
        // 查询账本
        List<BookUserQueryVo> bookUserResVoList = new ArrayList<>();
        if (bookIds.size() > 0) {
            bookUserResVoList = baseMapper.getAuditAll(new BookUserVo(bookIds, DataStatusEnum.DISABLE.getValue(), BookUserAuditStatusEnum.DEFAULT.getValue()));
        }
        return bookUserResVoList;
    }


    /**
     * @param
     * @return @return {@link List }<{@link BookUser }>
     * @author liang-hw
     * @description 查询我的申请
     * @date 2023/11/09 13:22:34
     */
    @Override
    public List<BookUserQueryVo> myApply(String userId) {
        // status状态为2
        List<String> bookIds = baseMapper.selectList(new QueryWrapper<BookUser>().lambda().eq(BookUser::getUserId, userId)).stream().map(x -> x.getBookId()).collect(Collectors.toList());
        if (bookIds.size() == 0) return new ArrayList<>();
        // 根据账本id分组
        List<BookUserQueryVo> bookUserResVoList = baseMapper.getAuditAll(new BookUserVo(bookIds, DataStatusEnum.DISABLE.getValue(), userId, BookUserAuditStatusEnum.DEFAULT.getValue()));
        return bookUserResVoList;
    }

    /**
     * @param @param userApplyBookReqVo
     * @return @return {@link BookUserResVo }
     * @author liang-hw
     * @description 用户从搜索中申请加入账本请求
     * @date 2023/11/09 13:21:02
     */
    @Override
    public BookUserResVo sendApply(UserApplyBookReqVo userApplyBookReqVo, String userId) {
        // 当前请求账本数据人是否是账本成员
        BookUser bookUser = baseMapper.selectOne((new QueryWrapper<BookUser>()).lambda().eq(BookUser::getUserId, userId).eq(BookUser::getBookId, userApplyBookReqVo.getBookId()).eq(BookUser::getStatus, DataStatusEnum.ENABLE.getValue()));
        if (null != bookUser) {
            logger.error("用户从搜索中申请加入账本请求你已是该成员，用户已经是账本成员，用户id：{}", UserTokenContextHolder.getUserTokenVOByToken().getUserId());
            throw new BasicInfoException(BasicInfoStatusEnum.UNSUPPORTED.getCode(), "你已是该成员");
        }
        BookResVo bookResVo = bookService.userAddBook(userApplyBookReqVo.getBookId(), userApplyBookReqVo.getRemark(), userId);
        BookUserResVo bookUserResVo = new BookUserResVo();
        BeanUtils.copyProperties(bookResVo, bookUserResVo);
        return bookUserResVo;
    }

    @Override
    public BookUserQueryVo getBookOne(String bookId, String userId) {
        BookUser bookUser = this.getOne(new QueryWrapper<BookUser>().lambda()
                .eq(BookUser::getStatus, DataStatusEnum.ENABLE.getValue())
                .eq(BookUser::getBookId, bookId)
                .eq(BookUser::getUserId, userId)
                .ne(BookUser::getAuditStatus, BookUserAuditStatusEnum.REJECT.getValue()));
        BookUserQueryVo bookUserQueryVo = mapperFacade.map(bookUser, BookUserQueryVo.class);
        return bookUserQueryVo;
    }
}

